﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class PreallocateRuleDetailDA : IPreallocateRuleDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LINE_ID = "@LINE_ID";
        private string PICK_CODE_SQL = "@PICK_CODE_SQL";
        private string PREALLOCATE_PICK_CODE = "@PREALLOCATE_PICK_CODE";
        private string PREALLOCATE_RULE_ID = "@PREALLOCATE_RULE_ID";
        private string PREALLOCATE_STRATEGY = "@PREALLOCATE_STRATEGY";
        private string SQL_CHECK_PREALLOCATE_RULE_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@PREALLOCATE_RULE_DETAIL WHERE PREALLOCATE_RULE_ID = @PREALLOCATE_RULE_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_ONE_PREALLOCATE_RULE_DETAIL = " DELETE FROM @_@PREALLOCATE_RULE_DETAIL WHERE  PREALLOCATE_RULE_ID = @PREALLOCATE_RULE_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_PREALLOCATE_RULE_DETAIL = " DELETE FROM @_@PREALLOCATE_RULE_DETAIL WHERE  PREALLOCATE_RULE_ID = @PREALLOCATE_RULE_ID ";
        private string SQL_INSERT_PREALLOCATE_RULE_DETAIL = " INSERT INTO @_@PREALLOCATE_RULE_DETAIL ( WH_ID, PREALLOCATE_RULE_ID, LINE_ID, DESCR, PREALLOCATE_STRATEGY, UOM_ID, PREALLOCATE_PICK_CODE, PICK_CODE_SQL, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @PREALLOCATE_RULE_ID, @LINE_ID, @DESCR, @PREALLOCATE_STRATEGY, @UOM_ID, @PREALLOCATE_PICK_CODE, @PICK_CODE_SQL, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_PREALLOCATE_RULE_DETAIL = " SELECT  A.ROWID, A.WH_ID, A.PREALLOCATE_RULE_ID, A.LINE_ID,A.DESCR, T2.[TEXT] AS UOM_DESCR, A.PREALLOCATE_STRATEGY,T.[TEXT] AS STRATEGY_DESCR, A.UOM_ID, A.PREALLOCATE_PICK_CODE,A.PICK_CODE_SQL,A.IS_ACTIVE,A.TS,A.CREATED_BY,A.CREATED_DATE,A.UPDATED_BY,A.UPDATED_DATE,B.Description  FROM  @_@PREALLOCATE_RULE_DETAIL A  LEFT JOIN @_@SYS_CODE_DETAIL B ON A.PREALLOCATE_STRATEGY = B.SYS_CODE AND B.SYS_CODE_TYPE = 'PreAllocStrategies' LEFT JOIN @_@SYS_CODE_DETAIL B2 ON A.UOM_ID = B.SYS_CODE AND B.SYS_CODE_TYPE = 'TUOM' LEFT JOIN sysadm.TRANSLATION T ON 'PreAllocStrategies.'+A.PREALLOCATE_STRATEGY = T.ITEM_ID  \t\t\tAND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE LEFT JOIN sysadm.TRANSLATION T2 ON 'TUOM.'+A.UOM_ID = T2.ITEM_ID  \t\t\tAND  T2.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T2.LANG+'-'+T2.COUNTRY = @LANGUAGE ";
        private string SQL_UPDATE_PREALLOCATE_RULE_DETAIL = " UPDATE @_@PREALLOCATE_RULE_DETAIL SET WH_ID = @WH_ID, DESCR = @DESCR, PREALLOCATE_STRATEGY = @PREALLOCATE_STRATEGY, UOM_ID = @UOM_ID, PREALLOCATE_PICK_CODE = @PREALLOCATE_PICK_CODE, PICK_CODE_SQL = @PICK_CODE_SQL, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE PREALLOCATE_RULE_ID = @PREALLOCATE_RULE_ID AND LINE_ID = @LINE_ID  ";
        private string TS = "@TS";
        private string UOM_ID = "@UOM_ID";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckPreallocateRuleDetailIDUnique(string preallocateRuleDetailID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateRuleDetailID), new SqlParameter("@Line_ID", lineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_PREALLOCATE_RULE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckPreallocateRuleDetailIDUnique(DataBase dataBase, DbTransaction tran, string preallocateRuleID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateRuleID), new SqlParameter("@Line_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_PREALLOCATE_RULE_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeletePreallocateRuleDetail(List<string> preallocateRuleDetailID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    string sql = this.SQL_DELETE_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID[i]);
                    for (int j = 0; j < preallocateRuleDetailID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateRuleDetailID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sql, paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeletePreallocateRuleDetail(List<string> preallocateRuleID, List<string> lineID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    string sql = this.SQL_DELETE_ONE_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID[i]);
                    for (int j = 0; j < preallocateRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateRuleID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sql, paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeletePreallocateRuleDetail(DataBase dataBase, DbTransaction tran, List<string> preallocateRuleDetailID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    string sql = this.SQL_DELETE_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID[i]);
                    for (int j = 0; j < preallocateRuleDetailID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateRuleDetailID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sql, paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeletePreallocateRuleDetail(DataBase dataBase, DbTransaction tran, List<string> preallocateRuleID, List<string> lineID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    string sql = this.SQL_DELETE_ONE_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID[i]);
                    for (int j = 0; j < preallocateRuleID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateRuleID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sql, paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllPreallocateRuleDetail(string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql, paras);
        }

        public DataSet GetAllPreallocateRuleDetail(DataBase dataBase, DbTransaction tran, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql, paras);
        }

        public PreallocateRuleDetailInfo GetPreallocateRuleDetailByID(string preallocateruledetailID, string lineID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE A.PREALLOCATE_RULE_ID = @PREALLOCATE_RULE_ID AND A.LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateruledetailID), new SqlParameter("@LINE_ID", lineID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            PreallocateRuleDetailInfo preallocateRuleDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    preallocateRuleDetailInfo = new PreallocateRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["PREALLOCATE_STRATEGY"].ToString(), reader["DESCR"].ToString(), reader["STRATEGY_DESCR"].ToString(), reader["UOM_ID"].ToString(), reader["UOM_DESCR"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["PICK_CODE_SQL"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return preallocateRuleDetailInfo;
        }

        public PreallocateRuleDetailInfo GetPreallocateRuleDetailByID(DataBase dataBase, DbTransaction tran, string preallocateruledetailID, string lineID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE A.PREALLOCATE_RULE_ID = @PREALLOCATE_RULE_ID AND A.LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateruledetailID), new SqlParameter("@LINE_ID", lineID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            PreallocateRuleDetailInfo preallocateRuleDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                preallocateRuleDetailInfo = new PreallocateRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["PREALLOCATE_STRATEGY"].ToString(), reader["DESCR"].ToString(), reader["STRATEGY_DESCR"].ToString(), reader["UOM_ID"].ToString(), reader["UOM_DESCR"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["PICK_CODE_SQL"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return preallocateRuleDetailInfo;
        }

        public DataSet GetPreallocateRuleDetailByQueryList(List<string> sqlWhere, PreallocateRuleDetailQueryEntity preallocateruledetailQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!preallocateruledetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, preallocateruledetailQuery.CurrentPage, preallocateruledetailQuery.PageSize, preallocateruledetailQuery.SortField, preallocateruledetailQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetPreallocateRuleDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, PreallocateRuleDetailQueryEntity preallocateruledetailQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!preallocateruledetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, preallocateruledetailQuery.CurrentPage, preallocateruledetailQuery.PageSize, preallocateruledetailQuery.SortField, preallocateruledetailQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<PreallocateRuleDetailInfo> GetPreallocateRuleDetailListByID(string preallocateRuleID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE A.PREALLOCATE_RULE_ID = @PREALLOCATE_RULE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateRuleID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            List<PreallocateRuleDetailInfo> preallocateRuleDetailInfos = new List<PreallocateRuleDetailInfo>();
            PreallocateRuleDetailInfo preallocateRuleDetail = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    preallocateRuleDetail = new PreallocateRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["PREALLOCATE_STRATEGY"].ToString(), reader["DESCR"].ToString(), reader["STRATEGY_DESCR"].ToString(), reader["UOM_ID"].ToString(), reader["UOM_DESCR"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["PICK_CODE_SQL"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    preallocateRuleDetailInfos.Add(preallocateRuleDetail);
                }
            }
            return preallocateRuleDetailInfos;
        }

        public List<PreallocateRuleDetailInfo> GetPreallocateRuleDetailListByID(DataBase dataBase, DbTransaction tran, string preallocateRuleID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID) + " WHERE A.PREALLOCATE_RULE_ID = @PREALLOCATE_RULE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREALLOCATE_RULE_ID", preallocateRuleID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            List<PreallocateRuleDetailInfo> preallocateRuleDetailInfos = new List<PreallocateRuleDetailInfo>();
            PreallocateRuleDetailInfo PreallocateRuleDetail = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                PreallocateRuleDetail = new PreallocateRuleDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["PREALLOCATE_RULE_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["PREALLOCATE_STRATEGY"].ToString(), reader["DESCR"].ToString(), reader["STRATEGY_DESCR"].ToString(), reader["UOM_ID"].ToString(), reader["UOM_DESCR"].ToString(), reader["PREALLOCATE_PICK_CODE"].ToString(), reader["PICK_CODE_SQL"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                preallocateRuleDetailInfos.Add(PreallocateRuleDetail);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return preallocateRuleDetailInfos;
        }

        public int InsertPreallocateRuleDetail(PreallocateRuleDetailInfo preallocateRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    preallocateRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_PreallocateRuleDetail_Parameters(preallocateRuleDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertPreallocateRuleDetail(DataBase dataBase, DbTransaction tran, PreallocateRuleDetailInfo preallocateRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    preallocateRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_PreallocateRuleDetail_Parameters(preallocateRuleDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_PreallocateRuleDetail_Parameters(PreallocateRuleDetailInfo preallocateRuleDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.PREALLOCATE_RULE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.DESCR, SqlDbType.VarChar, 100), new SqlParameter(this.PREALLOCATE_STRATEGY, SqlDbType.VarChar, 20), new SqlParameter(this.UOM_ID, SqlDbType.VarChar, 20), new SqlParameter(this.PREALLOCATE_PICK_CODE, SqlDbType.VarChar, 10), new SqlParameter(this.PICK_CODE_SQL, SqlDbType.VarChar, 0x7d0), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(preallocateRuleDetailInfo.WhID))
            {
                paramArray[0].Value = preallocateRuleDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = preallocateRuleDetailInfo.PreallocateRuleID;
            paramArray[2].Value = preallocateRuleDetailInfo.LineID;
            paramArray[3].Value = preallocateRuleDetailInfo.Descr;
            paramArray[4].Value = preallocateRuleDetailInfo.PreallocateStrategy;
            paramArray[5].Value = preallocateRuleDetailInfo.UomID;
            paramArray[6].Value = preallocateRuleDetailInfo.PreallocatePickCode;
            if (!string.IsNullOrEmpty(preallocateRuleDetailInfo.PickCodeSql))
            {
                paramArray[7].Value = preallocateRuleDetailInfo.PickCodeSql;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            paramArray[8].Value = preallocateRuleDetailInfo.IsActive;
            paramArray[9].Value = preallocateRuleDetailInfo.CreatedBy;
            paramArray[10].Value = preallocateRuleDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdatePreallocateRuleDetail(PreallocateRuleDetailInfo preallocateRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    preallocateRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_PreallocateRuleDetail_Parameters(preallocateRuleDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdatePreallocateRuleDetail(DataBase dataBase, DbTransaction tran, PreallocateRuleDetailInfo preallocateRuleDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    preallocateRuleDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_PreallocateRuleDetail_Parameters(preallocateRuleDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_PREALLOCATE_RULE_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

